package com.sixth.service.Impl;

//import com.github.pagehelper.PageHelper;
//import com.github.pagehelper.PageInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixth.dao.BranchDao;
import com.sixth.dao.StaffDao;
import com.sixth.dao.StafftransferDao;
import com.sixth.entity.Branch;
import com.sixth.entity.Staff;
import com.sixth.service.StaffService;
import com.sixth.util.CommonResult;
import com.sixth.util.TableLayUi;
import com.sixth.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: ssm002
 * @description:
 * @author:
 * @create: 2021-12-10 19:26
 **/
@Service(value = "staffService")
public class StaffServiceImpl implements StaffService {
    @Autowired
    private StaffDao staffDao;
    @Autowired
    private StafftransferDao success;
    @Autowired
    private BranchDao dao;

    //登录
    public CommonResult findByUserameAndPassword(Integer staffid, String password) {
        Staff staff=staffDao.selectByUserameAndPassword(staffid,password);
        if(staff==null){
            return new CommonResult(5000,"账号或密码错误",null);
        }
        if(staff.getState()==0){
            return new CommonResult(5000,"您已离职",null);
        }
        if(staff.getState()==2){
            return new CommonResult(5000,"该账号已冻结",null);
        }
        //在任意地方可以获取session对象。
        WebUtil.getSession().setAttribute("staff",staff);
        return new CommonResult(2000,"登录成功",null);
    }

    //根据头部条件查询全部
    public TableLayUi findStaffBycondition(int page, int limit,String staffname,Integer staffid, Integer branchid) {
        PageHelper.startPage(page,limit);
        List<Staff> staff= staffDao.selectStaffBycondition(staffname,staffid,branchid);
        PageInfo<Staff> pageInfo = new PageInfo<Staff>(staff);
        //pageInfo.getTotal:获取的为表中总条数
        //pageInfo.getList():获取的为当前页的记录数据
        return new TableLayUi(0,"",pageInfo.getTotal(),pageInfo.getList());
    }

    //离职员工
    public CommonResult updateState(int state, Integer id ) {
        System.out.println(state);
        Date date = new Date();
        Staff staff=staffDao.selectBoos(id);
        Integer branchid = staff.getBranchid();
        Integer deptid = staff.getDeptid();
        int i = staffDao.UpdateState(state, id,date);
        if (i>0){
            if ((deptid==1)){
               int j= dao.updateBoos(branchid);
               if(j>0){
                   return new CommonResult(2000,"修改成功",null);
               }
                return new CommonResult(5000,"修改失败",null);
            }
            return new CommonResult(2000,"修改成功",null);
        }
        return new CommonResult(5000,"修改失败",null);
    }

    @Override
    public CommonResult getDeptRole() {
        Staff staff = (Staff) WebUtil.getSession().getAttribute("staff");
        Integer staffid = staff.getStaffid();
        Map map = staffDao.selectByStaffid(staffid);
        return new CommonResult(2000,"成功",map);
    }

    @Override
    public CommonResult updatePassword(String password) {
        Staff staff = (Staff) WebUtil.getSession().getAttribute("staff");
        Integer staffid = staff.getStaffid();
        int i=staffDao.updatePassword(password,staffid);
        if (i>0){
            return  new CommonResult(2000,"修改成功",null );
        }
        return new CommonResult(5000,"修改失败",null );
    }

    //判断是否有店长
    public CommonResult position(Integer branchid) {

        Branch branch = dao.selectStaffidByBranchid(branchid);
        Integer staffid = branch.getStaffid();
        if(staffid==null)
        {
            return new CommonResult(0,"无店长",null);
        }
        return new CommonResult(1,"该店铺已有店长",null);
    }

    //判断是否有员工
    public CommonResult FindStaffid(Integer staffid) {
        Staff staff=staffDao.FindStaffid(staffid);
        if(staff!=null){
            return new CommonResult(2,"",null);
        }
        return new CommonResult(1,"",null);
    }

    public CommonResult getStaffIdcard(String idcard) {

        Staff staff=staffDao.getStaffIdcard(idcard);

        if(staff!=null){
            return new CommonResult(2,"",null);
        }
        return new CommonResult(1,"",null);
    }

    //判断手机号
    public CommonResult getStaffPhone(String phone) {
        Staff staff=staffDao.selectPhone(phone);
        if(staff!=null){
            return new CommonResult(2,"",null);
        }
        return new CommonResult(1,"",null);
    }

    //冻结
    public CommonResult updateStateDongjie(int state, Integer id) {
        int i=staffDao.updateStateDongjie(state,id);
        if (i>0){
            return new CommonResult(2000,"冻结成功",null);
        }
        return new CommonResult(5000,"冻结失败",null);
    }

    //解冻
    public CommonResult updateStateJieDong(int state, Integer id) {
        int i = staffDao.updateStateJieDong(state, id);
        if (i>0){
            return new CommonResult(2000,"解冻成功",null);
        }
        return new CommonResult(5000,"解冻失败",null);
    }

    //修改员工信息
    public CommonResult updatestaffmsg(String staffname,String address,String phone,Integer id) {
        int i = staffDao.updateBycondition(staffname,address,phone,id);
        if (i>0){
            return new CommonResult(2000,"修改成功",null);
        }
        return new CommonResult(5000,"修改失败",null);
    }

    //添加员工
    public CommonResult StaffAdd(Integer staffid, String staffname, String password, String idcard, String address, String phone, Date entrytime, Integer state, Integer roleid, String url, Integer branchid) {

        Date date = new Date();
        entrytime=date;
       int i= staffDao.insertStaff(staffid,staffname,password,idcard,address,phone,entrytime,state,roleid,url,branchid);
        if (i>0){
            if(roleid==1){
                int j=dao.UpdateBoss(staffid,branchid);
                if (j>0){
                    return new CommonResult(2000,"添加成功",null);
                }
                return new CommonResult(5000,"添加失败",null);
            }
            return new CommonResult(2000,"添加成功",null);
        }
        return new CommonResult(5000,"添加失败",null);
    }

    //调职审核通过
    public CommonResult ExPass(Integer id, Integer newbranchid, Integer newdeptid, Integer staffid) {
        Staff staff = (Staff) WebUtil.getSession().getAttribute("staff");
        //登陆人id
        Integer staffid1 = staff.getStaffid();
        Date date = new Date();
        Integer ii = success.ExPass(id,date , staffid1);

        //查询员工之前的部门和店铺
        Staff oldstaff=staffDao.selectBystaffid(staffid);

        Integer oldstaffBranchid = oldstaff.getBranchid();
        Integer oldstaffDeptid = oldstaff.getDeptid();
        Integer oldstaffRoleid = oldstaff.getRoleid();
        Integer oldstaffStaffid = oldstaff.getStaffid();


        Integer i = staffDao.StaffTransfer(newbranchid, newdeptid, staffid);
        CommonResult result;
        if (i > 0) {
            //添加到调动信息
            Integer j =staffDao.insertNewStaff(oldstaffStaffid,oldstaffBranchid,oldstaffDeptid,oldstaffRoleid,newbranchid,newdeptid,date,staffid1);
            if(j>0){
                result = new CommonResult(1, "操作成功", null);
            }else {
                result =new CommonResult(2,"操作失败",null);
            }
        } else {
            result =new CommonResult(2,"操作失败",null);
        }
        return result;
    }

    //调职驳回
    public CommonResult ExReject(Integer id) {
        Staff staff = (Staff) WebUtil.getSession().getAttribute("staff");
        Integer staffid = staff.getStaffid();
        Integer i = success.ExReject(id, new Date(), staffid);
        CommonResult result;
        if (i > 0) {
            result = new CommonResult(1, "驳回成功", null);
        } else {
            result =new CommonResult(2,"驳回失败",null);
        }
        return result;
    }

    public List<Map> getStaff() {
        return staffDao.getStaff();
    }

    public CommonResult getStaffs() {
        List<Staff>list= staffDao.getStaffs();
        return new CommonResult(2000,"查询店长姓名成功",list);
    }

}
